package com.example.juejincheck.service;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.juejincheck.bean.FeidianMsgInfoResp;
import com.example.juejincheck.bean.FeidianResp;
import com.example.juejincheck.bean.SendMsgDTO;
import com.example.juejincheck.bean.WeiZhiMiShuVO;
import com.example.juejincheck.domain.FeidianConfig;
import com.example.juejincheck.domain.JuejinAssignAccount;
import com.example.juejincheck.utils.Constants;
import com.example.juejincheck.utils.DateUtils;
import com.example.juejincheck.utils.LunarUtil;
import com.example.juejincheck.utils.TianxingUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yangkun
 * @description
 * @date 2022年11月26日 16:11
 */
@Service
public class MiniprogramService {
    private static final Logger log = LoggerFactory.getLogger(MiniprogramService.class);

    private static String MSG_URL= "https://www.pushplus.plus/send/";
    public void sendMsg(JuejinAssignAccount assignAccount, FeidianConfig feidianConfig) {
        if(!StringUtils.isEmpty(assignAccount.getPushPlusToken())){
            StringBuffer html = new StringBuffer();
            html.append("<h5>"+feidianConfig.getName()+"</h5>")
                    .append("<span style='white-space: pre-line'>活动规则："+feidianConfig.getContent()+"</span>");
            if(!StringUtils.isEmpty(feidianConfig.getThemeTitle())){
                html.append("<p>圈子是："+feidianConfig.getThemeTitle()+" </p>");
            }
            if(!StringUtils.isEmpty(feidianConfig.getTopicTitle())){
                html.append("<p>话题是："+feidianConfig.getTopicTitle()+" </p>");
            }
            html.append("</div>");
            Map<String,Object> map = new HashMap<>();
            map.put("token",assignAccount.getPushPlusToken());
            map.put("title","沸点活动未打卡通知，不要忘记了哦");
            map.put("content",html.toString());
            log.info("参数：{}", map);
            String msgUrl = HttpUtil.post(MSG_URL,map);
            log.info("发送消息：{}", msgUrl);
        }else{
            String url = Constants.WEI_MI_SHU_API + "/openapi/v1/chat/contact";
            WeiZhiMiShuVO weiZhiMiShuVO = new WeiZhiMiShuVO();
            //发送文字信息
            weiZhiMiShuVO.setType(1);
            StringBuffer html = new StringBuffer();
            html.append("你还没发布沸点，活动记得参加哦\n"+feidianConfig.getName()+"\n")
                    .append("活动规则：\n")
                    .append(feidianConfig.getContent());
            weiZhiMiShuVO.setContent(html.toString());
            JSONObject object = new JSONObject();
            object.put("apiKey", Constants.API_KEY);
            //好友昵称
            object.put("name", assignAccount.getOpenId());
            //好友备注
            object.put("alias", assignAccount.getNickName());
            object.put("message", weiZhiMiShuVO);
            String body = HttpUtil.createPost(url)
                    .body(JSON.toJSONString(object))
                    .execute()
                    .body();
            System.out.println("传递的参数是：" + JSON.toJSONString(object));
            System.out.println("发送成功了吗?" + body);
        }
    }
    public void sendCheckInMsg(String token, String lottery) {
        if(!StringUtils.isEmpty(token)){
            StringBuffer html = new StringBuffer();
            Map<String,Object> map = new HashMap<>();
            map.put("token", token);
            map.put("title","墨天伦自动化打卡通知");

            html.append("<h4>自动化签到详细信息：</h4>");
            html.append("<p>已签到</p>");
            if(!StringUtils.isEmpty(lottery)){
                html.append("<p>"+lottery+"</p>");
            }
            map.put("content",html.toString());
            log.info("参数：{}", map);
            String msgUrl = HttpUtil.post(MSG_URL,map);
            log.info("发送消息：{}", msgUrl);
        }
    }


    /**
     * 每天将指定信息推送到微信个人
     */
    public void sendWechatToUser() {
        String url = Constants.WEI_MI_SHU_API + "/openapi/v1/chat/contact";
        WeiZhiMiShuVO weiZhiMiShuVO = new WeiZhiMiShuVO();
        //发送文字信息
        weiZhiMiShuVO.setType(1);
        //拿到发送的消息列表
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat format_h = new SimpleDateFormat("yyyy-MM-dd");
        //今天的日期
        String toDate = format_h.format(new Date());
        //相恋的日期
        String lian = "2020-05-31";
        //两个日期相差的天数
        int cha = DateUtils.getDayByTwoDay(toDate, lian);
        String todayDate = format.format(new Date());

        //根据阳历计算农历
        Calendar today = Calendar.getInstance();
        LunarUtil lunar = new LunarUtil(today);
        String nongli = lunar.toString();
        String week = DateUtils.getDaysByWeekChina(toDate);
        StringBuilder sb = new StringBuilder();
        sb.append("亲爱的老婆，早~\n\n")
                .append("充满希望的一天又开始了，今天也要一直开心，不能生我气气~")
                .append("\n\n今天是：")
                .append(todayDate)
                .append(" 星期")
                .append(week)
                .append(" 农历")
                .append(nongli)
                .append("\n")
                .append("是我们相爱的第")
                .append(cha)
                .append("天")
                .append("\n\n")

        ;
        JSONObject tianqiObject = TianxingUtils.getWeather();
        sb.append("【天气预报】")
                .append(tianqiObject.getString("weather"))
                .append("\n")
                .append("当前气温：")
                .append(tianqiObject.getString("real"))
                .append("\n")
                .append("今天最高温度：")
                .append(tianqiObject.getString("highest"))
                .append("\n")
                .append("最低温度：")
                .append(tianqiObject.getString("lowest"))
                .append("\n")
                .append("风向：")
                .append(tianqiObject.getString("wind"))
                .append("\n")
                .append("空气质量：")
                .append(tianqiObject.getString("quality"))
                .append("\n")
                .append("空气质量指数：")
                .append(tianqiObject.getString("aqi"))
                .append("\n")
                .append("小提示：")
                .append(tianqiObject.getString("tips"));

        //土味情话
        sb.append("\n\n")
                .append("【爱你一日言】")
                .append(TianxingUtils.getLoveWords())
                .append("\n\n【爱你一日古诗词】")
                .append(TianxingUtils.getQingshiWords())
                .append("\n\n【早安寄语】")
                .append(TianxingUtils.getGoodMorning());
        weiZhiMiShuVO.setContent(sb.toString());
        JSONObject object = new JSONObject();
        object.put("apiKey", Constants.API_KEY);
        //好友昵称
        object.put("name", "与 卿");
        //好友备注
        object.put("alias", "老婆");
        object.put("message", weiZhiMiShuVO);
        String body = HttpUtil.createPost(url)
                .body(JSON.toJSONString(object))
                .execute()
                .body();
        System.out.println("传递的参数是：" + JSON.toJSONString(object));
        System.out.println("发送成功了吗?" + body);
    }

}
